package com.hunt.controller;

import com.hunt.model.entity.FinanceInfoNew;
import com.hunt.model.entity.FinanceInfoNewDetail;
import com.hunt.service.FinanceNewsService;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author sjs
 * @Date 2020/11/13
 * @Description
 */
@Api(value = "手机app端,新闻管理模块")
@Controller
@RequestMapping(path = "/app/news")
public class FinanceNewsController extends BaseController {

    @Autowired
    @Resource
    private FinanceNewsService financeNewsService;

    @ApiOperation(value = "新闻财务列表查询", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getFinanceInfo", method = RequestMethod.GET)
    public Result findNewsQuick(@RequestParam(value = "time",defaultValue = "",required = false) String time,
                                @RequestParam(value = "sizeUP", defaultValue = "10") Integer sizeUP) {
        List<FinanceInfoNew> financeInfoList = financeNewsService.findFinanceInfoByPage(time,sizeUP);
        if(financeInfoList != null){
            for (FinanceInfoNew infoNew : financeInfoList){
                String title = infoNew.getTitle().replace("\\r\\n", "").trim();
                infoNew.setTitle(title);
            }
        }

        return Result.success(financeInfoList);
    }

    @ApiOperation(value = "新闻财务详情查询", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getFinanceInfoDetail", method = RequestMethod.GET)
    public Result getFinanceNewDetail(@RequestParam(value = "detailId") String detailId) {
        FinanceInfoNewDetail detail = financeNewsService.findFinanceDetailByDetailId(detailId);
        return Result.success(detail);
    }
    @ApiOperation(value = "新闻财务详情网页", httpMethod = "GET",produces = "text/html")
    @RequestMapping(value = "getFinanceInfoDetailPage", method = RequestMethod.GET)
    public String getFinanceNewDetailPage(@RequestParam(value = "detailId") String detailId, Model model) {
        FinanceInfoNewDetail detail = financeNewsService.findFinanceDetailByDetailId(detailId);
        model.addAttribute("detail",detail);
        return "pc/oldpage";
    }
    @ApiOperation(value = "新闻财务详情网页新", httpMethod = "GET",produces = "text/html")
    @RequestMapping(value = "getFinanceInfoDetailNewPage", method = RequestMethod.GET)
    public String getFinanceInfoDetailOldPage(@RequestParam(value = "detailId") String detailId, Model model) {
        FinanceInfoNewDetail detail = financeNewsService.findFinanceDetailByDetailId(detailId);
        model.addAttribute("detail",detail);
        return "pc/newpage";
    }
    @ApiOperation(value = "新闻财务列表查询pc", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getFinanceInfoPc", method = RequestMethod.GET)
    public Result findNewsQuickPc(@RequestParam(value = "time",defaultValue = "",required = false) String time,
                                @RequestParam(value = "sizeUP", defaultValue = "10") Integer sizeUP) {
        if(!time.equals("")){
            time=datefmt(time);
        }

        List<FinanceInfoNew> financeInfoList = financeNewsService.findFinanceInfoByPage(time,sizeUP);
        if(financeInfoList != null){
            for (FinanceInfoNew infoNew : financeInfoList){
                String title = infoNew.getTitle().replace("\\r\\n", "").trim();
                infoNew.setTime(datefmtreverse(infoNew.getTime()));
                infoNew.setTitle(title);
            }
        }

        return Result.success(financeInfoList);
    }
    private String datefmt(String time) {
        String befor=time.substring(0,2);
        String middle=time.substring(2,4);
        String after=time.substring(4,6);
        String after1=time.substring(6,8);
        time=befor+"月"+middle+"日"+" "+after+":"+after1;
        return  time;
    }
    private String datefmtreverse(String time) {
        String month=time.substring(0,2);
        String day=time.substring(3,5);
        String hour=time.substring(7,9);
        String minute=time.substring(10,12);
        time=month+day+hour+minute;
        return  time;
    }
}
